<template>
  <header class="header">
    <div class="header-left">
      <img src="@/assets/logo.jpg" alt="东软之声广播台" class="logo" />
      <h1>大连东软信息学院东软之声广播台</h1>
    </div>
    <div class="header-right">
      <button class="return-btn" @click="handleReturn">返回</button>
      <div class="user-info" v-if="authStore.isLoggedIn">
        <span class="username">{{ authStore.user.username }}</span>
        <span class="user-role">{{ roleDisplay }}</span>
        <button class="logout-btn" @click="handleLogout">退出</button>
      </div>
      <button v-else class="login-btn" @click="handleLogin">登录</button>
    </div>
  </header>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { computed } from 'vue'

const authStore = useAuthStore()
const router = useRouter()

// 初始化时检查登录状态
authStore.initialize()

const roleDisplay = computed(() => {
  const roles = {
    member: '部员',
    minister: '部长',
    admin: '管理员'
  }
  return roles[authStore.user?.role] || ''
})

const handleLogin = () => {
  router.push('/login')
}

const handleLogout = () => {
  authStore.logout()
  // 可以添加跳转到首页的逻辑
  router.push('/')
}

const handleReturn = () => {
  router.push('/')
}
</script>

<style scoped>
/* 全屏自适应头部样式 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
  color: white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  position: sticky;
  top: 0;
  z-index: 1000;
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  width: 100%;
  min-height: 70px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logo {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.8);
}

.header h1 {
  font-size: 1.8rem;
  margin: 0;
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  gap: 15px;
}

button {
  padding: 8px 18px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.return-btn {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
}

.return-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.login-btn {
  background-color: #4CAF50;
  color: white;
}

.login-btn:hover {
  background-color: #45a049;
  transform: translateY(-2px);
}

/* 响应式设计优化 */
@media (max-width: 1200px) {
  .header {
    padding: 14px 20px;
  }
  
  .header h1 {
    font-size: 1.6rem;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 12px 16px;
    flex-wrap: wrap;
    gap: 10px;
  }
  
  .header-left {
    flex: 1;
    min-width: 0;
  }
  
  .header h1 {
    font-size: 1.3rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .logo {
    width: 45px;
    height: 45px;
    flex-shrink: 0;
  }
  
  .header-right {
    flex-shrink: 0;
  }
}

@media (max-width: 600px) {
  .header {
    padding: 10px 12px;
    min-height: 60px;
  }
  
  .header h1 {
    font-size: 1.1rem;
    display: none; /* 在小屏幕上隐藏标题 */
  }
  
  .logo {
    width: 40px;
    height: 40px;
  }
  
  .header-right {
    gap: 8px;
  }
  
  button {
    padding: 6px 12px;
    font-size: 0.9rem;
  }
  
  .user-info {
    flex-direction: column;
    gap: 4px;
    align-items: flex-end;
  }
  
  .username {
    font-size: 0.9rem;
  }
  
  .user-role {
    font-size: 0.7rem;
    padding: 1px 6px;
  }
}

@media (max-width: 480px) {
  .header {
    padding: 8px 10px;
  }
  
  .header-left {
    gap: 8px;
  }
  
  .logo {
    width: 35px;
    height: 35px;
  }
  
  .header-right {
    gap: 6px;
  }
  
  button {
    padding: 4px 8px;
    font-size: 0.8rem;
  }
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  color: white;
}

.username {
  font-weight: bold;
}

.user-role {
  background-color: rgba(255, 255, 255, 0.2);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.logout-btn {
  background-color: #f44336;
  color: white;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 0.9rem;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: #d32f2f;
}
</style>